package com.njupt.controller;

import com.njupt.po.Student;
import com.njupt.service.StudentService;
import com.njupt.util.HavingLogin;
import com.njupt.util.MyUtil;
import com.njupt.util.UserNode;
import com.njupt.vo.LoginVo;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;
import java.util.UUID;

import java.util.List;

@RestController
public class StudentController {
    @Resource
    private StudentService studentService;

    @GetMapping("getcode")
    public Integer getCode(HttpSession session)
    {
        Integer code=(int)(Math.random()*10000);
        session.setAttribute("code", code);
        return code;
    }



    @PostMapping("login")
    public LoginVo login(String stuID, String pwd,
                         HttpSession session, HttpServletRequest request)
    {
        LoginVo vo=new LoginVo();
        Integer code=(Integer)session.getAttribute("code");
        pwd= MyUtil.encode(pwd, -code);
        System.out.println("pwd="+pwd);
        Student stu=studentService.login(stuID, pwd);
        //接下来要判断是否已经存在登录用户
        if(stu==null)
        {
            vo.setCode(100);
            vo.setMessage("userID or pwd is error!");
            return vo;
        }
        /*
         *  要验证该账号是否在别的地方已登录
         */
        UserNode node= HavingLogin.check(stuID);
        if(node==null)
        {
            node=new UserNode();
            node.setStuID(stuID);
            //node.setSession(session);
            node.setLoginTime(MyUtil.getTime());
            node.setIp(request.getRemoteAddr());
            HavingLogin.add(node);
            vo.setCode(200);
            vo.setMessage("success");
            return vo;
        }
        //存在已登录
        vo.setCode(300);
        String uuid=UUID.randomUUID().toString();
        session.setAttribute(uuid, stuID);
        vo.setUuid(uuid);
        vo.setMessage(node.getLoginTime()+" "+node.getIp());
        return vo;
    }


    @GetMapping("/view")
    public List<Student> getAll(){
        return studentService.queryAllStudent();
    }

    @GetMapping("/searchstudent")
    public List<Student> getStudent(@RequestParam("name") String name,
                                    @RequestParam(value="majorID",required = false) Integer majorID){
        Student student = new Student();
        student.setName(name);
        if (majorID == null ) {majorID = 0;}
        student.setMajorID(majorID);
        System.out.println(student.getMajorID());
        return studentService.queryStudent(student);
    }

//    @GetMapping("/searchbymajorid")
//    public List<Student> getStudentByMajor(@RequestParam Integer majorID){
//        if(majorID == null) return studentService.queryAllStudent();
//        return studentService.queryStudentByMajorID(majorID);
//    }

    @PostMapping("/newstudent")
    public Integer newStudent(@RequestParam String name,@RequestParam String enrollTime,
                              @RequestParam Integer majorID,@RequestParam String sex,
                              @RequestParam String sfzh){
        String stuID=studentService.maxStuID();
        Student student=new Student();
        student.setStuID(stuID);
        student.setName(name);
        student.setMajorID(majorID);
        student.setEnrollTime(enrollTime);
        student.setSex(sex);
        student.setSfzh(sfzh);
        return studentService.newStudent(student);
    }

    @GetMapping("/maxstuid")
    public String maxStuID(){
        return studentService.maxStuID();
    }

    @GetMapping("/deletestudent")
    public int deleteStudent(String stuID){
        return studentService.deleteStudent(stuID);
    }

    @GetMapping("/modifystudent")
    public int modifystudent(String stuID,Integer majorID){
        return studentService.modifyStudent(stuID,majorID);
    }
}
